package graphics.object.testing;

import graphics.TestingGraphics;
import graphics.GLObjectManager.RenderPriority;
import graphics.object.GLTextRectangle;
import graphics.object.interfaces.GLKeyboardListener;
import graphics.object.interfaces.GLMouseListener;
import graphics.object.interfaces.GLTextListener;
import graphics.object.interfaces.GLTickListener;

import org.lwjgl.input.Keyboard;
import org.newdawn.slick.Color;

public class GLWritableTextRectangle extends GLTextRectangle implements GLKeyboardListener, GLMouseListener, GLTextListener, GLTickListener{

    private static final long serialVersionUID = 1L;

    public Color textColor = new Color(255, 255, 255);
    public Color selectedColor = new Color(150, 150, 255);
    public Color renderColor = textColor;

    private boolean isSelected = false;
    private boolean isFlashOn = false;
    private long lastMove = 0;

    public GLWritableTextRectangle(String text, int x, int y){
        super(text, x, y);
        setRenderPriority(RenderPriority.LOW);
        lastMove = TestingGraphics.getTime();
    }

    public void onRender() {        
        TestingGraphics.font.drawString(x, y, text, renderColor);
    }

    public void onMousePress(int id, int x, int y) {
        if(id == 0 && contains(x, y)){
            isSelected = !isSelected;
            if(isSelected){
            	lastMove = TestingGraphics.getTime();
                renderColor = selectedColor;
            }else{
                if(isFlashOn){
                    setText(getText().substring(0, getText().length() - 1));
                    isFlashOn = false;
                }
                if(getText().length() == 0){
                    setText("EMPTY");
                }
                renderColor = textColor;
            }
        }
    }

    public void onKeyPress(int id) {
        if(isSelected){
            if(id == 14 && getText().length() > 0){
                if(isFlashOn){
                    setText(getText().substring(0, getText().length() - 2) + "|");
                }else{                    
                    setText(getText().substring(0, getText().length() - 1));
                }
            }
            if(id == 28 && !(Keyboard.isKeyDown(42) || Keyboard.isKeyDown(54))){
                isSelected = false;
                if(isFlashOn){
                    setText(getText().substring(0, getText().length() - 1));
                    isFlashOn = false;
                }
                if(getText().length() == 0){
                    setText("EMPTY");
                }
                renderColor = textColor;
            }
        }
    }

    public void onTextInput(String input) {
        if(isSelected){
            if(isFlashOn){
                setText(getText().substring(0, getText().length() - 1) + input + "|");
            }else{                
                setText(getText() + input);
            }
        }
    }

    public void onMouseMove(int x, int y, int dx, int dy) {}
    public void onMouseRelease(int id, int x, int y) {}
    public void onKeyRelease(int id) {}

    public void onTick(long currentTime, long deltaTime) {
        if(isSelected && (currentTime - lastMove > 500)){
            lastMove += 500;
            if(isFlashOn){
                setText(getText().substring(0, getText().length() - 1));
                isFlashOn = false;
            }else{
                setText(getText() + "|");
                isFlashOn = true;
            }
        }
    }
}
